// @ts-ignore
/* eslint-disable */
import dayjs from 'dayjs';
import { request } from 'umi';

const influx_request = (qin: string) =>
  request<any>('http://greeshare.gree.com/v1/api/influx', {
    // request<API.Response>('http://192.168.3.81:8088/v1/api/influx', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: {
      Oper: 'query', // 操作名称
      fluxstring: qin,
    },
  });

// 使用timeShift(duration: 8h, columns: ["_start", "_stop", "_time"])调整时间
export async function getGreeProductStatesTimeChange(barcode: string, deviceImsi: string) {
  // let barcode = "123456789";
  // let deviceImsi = "46006304228299"
  let qin = `from(bucket: "yjy")
      |> range(start: -3h)
      |> filter(fn: (r) => r["_measurement"] == "ZC")
      |> filter(fn: (r) => r["Barcode"] == "${barcode}")
      |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
      |> filter(fn: (r) => r["_field"] == "CcompressHz" or r["_field"] == "Stemper_in_pipe")
      |> timeShift(duration: 8h, columns: ["_start", "_stop", "_time"])
      |> drop(columns: ["_measurement","DeviceImsi","_start","_stop","result","table"])
      |> yield()
  `;
  //return request<API.Response>('http://192.168.3.81:8088/v1/api/influx', {
  return request<any>('http://greeshare.gree.com/v1/api/influx', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: {
      Oper: 'query', // 操作名称
      fluxstring: qin,
    },
  });
}

// ****************************influx flux ***********************************

// 全部，数据用dayjs转化，不用加duration: 8h
export async function getProductStates(t: number, deviceImsi: string) {
  // let barcode = "123456789";
  // let deviceImsi = "46006304228299"
  let qin = `from(bucket: "yjy")
		|> range(start: -${t}h)
		|> filter(fn: (r) => r["_measurement"] == "ZC")
		|> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
		|> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
		|> yield()
	`;
  return influx_request(qin);
}

// 数据用dayjs转化，不用加duration: 8h
export async function getProductStates_Hz(t: number, deviceImsi: string) {
  // let barcode = "123456789";
  // let deviceImsi = "46006304228299"
  let qin = `from(bucket: "yjy")
      |> range(start: -${t}h)
      |> filter(fn: (r) => r["_measurement"] == "ZC")
      |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
      |> filter(fn: (r) => r["_field"] == "CcompressHz" or r["_field"] == "Scompress_hz")
      |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
      |> yield()
  `;
  return influx_request(qin);
}

export async function getProductStates_temper(t: number, deviceImsi: string) {
  // let barcode = "123456789";
  // let deviceImsi = "46006304228299"
  let qin = `from(bucket: "yjy")
      |> range(start: -${t}h)
      |> filter(fn: (r) => r["_measurement"] == "ZC")
      |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
      |> filter(fn: (r) => r["_field"] == "Ctemper" or r["_field"] == "Stemper_in" or r["_field"] == "Stemper_out" or r["_field"] == "Stemper_in_pipe" or r["_field"] == "Stemper_ex")
      |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
      |> yield()
  `;
  console.log(qin);
  return influx_request(qin);
}

// 开关类状态
export async function getProductStates_sw(t: number, deviceImsi: string) {
  // let barcode = "123456789";
  // let deviceImsi = "46006304228299"
  let qin = `from(bucket: "yjy")
      |> range(start: -${t}h)
      |> filter(fn: (r) => r["_measurement"] == "ZC")
      |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
      |> filter(fn: (r) => r["_field"] == "Con" or r["_field"] == "Cenergy_on" or r["_field"] == "Scompress_on" or r["_field"] == "Sfun_in_on" or r["_field"] == "Sfun_out_on")
      |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
      |> yield()
  `;
  console.log(qin);
  return influx_request(qin);
}

// 开关类状态
export async function getProductStates_sw2(t: number, deviceImsi: string) {
  // let barcode = "123456789";
  // let deviceImsi = "46006304228299"
  let qin = `from(bucket: "yjy")
      |> range(start: -${t}s)
      |> filter(fn: (r) => r["_measurement"] == "ZC")
      |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
      |> filter(fn: (r) => r["_field"] == "Con" or r["_field"] == "Cenergy_on" or r["_field"] == "Scompress_on" or r["_field"] == "Sfun_in_on" or r["_field"] == "Sfun_out_on")
      |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
      |> yield()
  `;
  console.log(qin);
  return influx_request(qin);
}

// 所有数据
export async function getProductStatesAll(t: number, unit: string, deviceImsi: string) :Promise<API.InfluxDataAllRes> {
  let qinsw = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "err" or r["_field"] == "Con" or r["_field"] == "Cenergy_on" or r["_field"] == "Scompress_on" or r["_field"] == "Sfun_in_on" or r["_field"] == "Sfun_out_on")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;

  let qintemper = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "Ctemper" or r["_field"] == "Stemper_in" or r["_field"] == "Stemper_out" or r["_field"] == "Stemper_in_pipe" or r["_field"] == "Stemper_ex")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinhz = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "CcompressHz" or r["_field"] == "Scompress_hz")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qina = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "SDC_A")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinv = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "SDC_V")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinw = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "SDC_W")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinerr = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "Etemper_in" or r["_field"] == "Etemper_out" or r["_field"] == "Etemper_in_pipe" or r["_field"] == "Eetemper_out_pipe" or r["_field"] == "Efun_in" or r["_field"] == "Efun_out" or r["_field"] == "Ec_m_d" or r["_field"] == "Ec_m_o" or r["_field"] == "Eovercurrent" or r["_field"] == "Eoverpower" or r["_field"] == "Eovervoltage_m" or r["_field"] == "Elowvoltage_m" or r["_field"] == "Eovercurrent_w" or r["_field"] == "Elow_v_Battery" or r["_field"] == "Ehigh_v_Battery" or r["_field"] == "Eexhaust" or r["_field"] == "Efreezing" or r["_field"] == "Efluorine")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  return request<API.InfluxDataAllRes>('http://greeshare.gree.com/v1/api/getProductStatesAll', {
    // request<API.Response>('http://192.168.3.81:8088/v1/api/influx', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: {
     // times: t, // 时间：秒
     // deviceImsi: deviceImsi,
      qinsw:qinsw,
      qintemper:qintemper,
      qinhz:qinhz,
      qina:qina,
      qinv:qinv,
      qinw:qinw,
      qinerr:qinerr,
    },
  });
}

// 定时刷新所有数据
export async function getProductStatesAllRefreshByImsi(t: number, unit: string, deviceImsi: string, viewMode: number) :Promise<API.InfluxDataAllRes> {
  if (viewMode === 0) {
    const a:API.InfluxDataAllRes = {}
    return a;
  } 
  let qinsw = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "err" or r["_field"] == "Con" or r["_field"] == "Cenergy_on" or r["_field"] == "Scompress_on" or r["_field"] == "Sfun_in_on" or r["_field"] == "Sfun_out_on")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;

  let qintemper = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "Ctemper" or r["_field"] == "Stemper_in" or r["_field"] == "Stemper_out" or r["_field"] == "Stemper_in_pipe" or r["_field"] == "Stemper_ex")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinhz = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "CcompressHz" or r["_field"] == "Scompress_hz")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qina = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "SDC_A")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinv = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "SDC_V")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinw = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "SDC_W")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinerr = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "Etemper_in" or r["_field"] == "Etemper_out" or r["_field"] == "Etemper_in_pipe" or r["_field"] == "Eetemper_out_pipe" or r["_field"] == "Efun_in" or r["_field"] == "Efun_out" or r["_field"] == "Ec_m_d" or r["_field"] == "Ec_m_o" or r["_field"] == "Eovercurrent" or r["_field"] == "Eoverpower" or r["_field"] == "Eovervoltage_m" or r["_field"] == "Elowvoltage_m" or r["_field"] == "Eovercurrent_w" or r["_field"] == "Elow_v_Battery" or r["_field"] == "Ehigh_v_Battery" or r["_field"] == "Eexhaust" or r["_field"] == "Efreezing" or r["_field"] == "Efluorine")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  return request<API.InfluxDataAllRes>('http://greeshare.gree.com/v1/api/getProductStatesAll', {
    // request<API.Response>('http://192.168.3.81:8088/v1/api/influx', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: {
     // times: t, // 时间：秒
     // deviceImsi: deviceImsi,
      qinsw:qinsw,
      qintemper:qintemper,
      qinhz:qinhz,
      qina:qina,
      qinv:qinv,
      qinw:qinw,
      qinerr:qinerr,
    },
  });
}


// 定时刷新所有数据
export async function getProductStatesAllRefreshById(t: number, unit: string, Id: number, viewMode: number) :Promise<API.InfluxDataAllRes> {
  if (viewMode === 0) {
    const a:API.InfluxDataAllRes = {}
    return a;
  } 
  let qinsw = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["ID"] == "${Id}")
  |> filter(fn: (r) => r["_field"] == "err" or r["_field"] == "Con" or r["_field"] == "Cenergy_on" or r["_field"] == "Scompress_on" or r["_field"] == "Sfun_in_on" or r["_field"] == "Sfun_out_on")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;

  let qintemper = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["ID"] == "${Id}")
  |> filter(fn: (r) => r["_field"] == "Ctemper" or r["_field"] == "Stemper_in" or r["_field"] == "Stemper_out" or r["_field"] == "Stemper_in_pipe" or r["_field"] == "Stemper_ex")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinhz = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["ID"] == "${Id}")
  |> filter(fn: (r) => r["_field"] == "CcompressHz" or r["_field"] == "Scompress_hz")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qina = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["ID"] == "${Id}")
  |> filter(fn: (r) => r["_field"] == "SDC_A")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinv = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["ID"] == "${Id}")
  |> filter(fn: (r) => r["_field"] == "SDC_V")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinw = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["ID"] == "${Id}")
  |> filter(fn: (r) => r["_field"] == "SDC_W")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinerr = `from(bucket: "yjy")
  |> range(start: -${t}${unit})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["ID"] == "${Id}")
  |> filter(fn: (r) => r["_field"] == "Etemper_in" or r["_field"] == "Etemper_out" or r["_field"] == "Etemper_in_pipe" or r["_field"] == "Eetemper_out_pipe" or r["_field"] == "Efun_in" or r["_field"] == "Efun_out" or r["_field"] == "Ec_m_d" or r["_field"] == "Ec_m_o" or r["_field"] == "Eovercurrent" or r["_field"] == "Eoverpower" or r["_field"] == "Eovervoltage_m" or r["_field"] == "Elowvoltage_m" or r["_field"] == "Eovercurrent_w" or r["_field"] == "Elow_v_Battery" or r["_field"] == "Ehigh_v_Battery" or r["_field"] == "Eexhaust" or r["_field"] == "Efreezing" or r["_field"] == "Efluorine")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  return request<API.InfluxDataAllRes>('http://greeshare.gree.com/v1/api/getProductStatesAll', {
    // request<API.Response>('http://192.168.3.81:8088/v1/api/influx', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: {
     // times: t, // 时间：秒
     // deviceImsi: deviceImsi,
      qinsw:qinsw,
      qintemper:qintemper,
      qinhz:qinhz,
      qina:qina,
      qinv:qinv,
      qinw:qinw,
      qinerr:qinerr,
    },
  });
}


// 所有数据 按id查询
export async function getProductStatesAllViewById(Id: number, viewValues: API.ViewValues) :Promise<API.InfluxDataAllRes> {
  let timestring:string;
  if (viewValues.viewMode===1) {
    timestring = `start: -${viewValues.forepartTime}m`;
  }else {
    timestring =`start: ${viewValues.startTime}, stop: ${viewValues.stopTime}`
  }
/*   from(bucket:"example-bucket")
  |> range(start: 1527031800, stop: 1527033600)
  // ...
   */
  let qinsw = `from(bucket: "yjy")
  |> range(${timestring})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["ID"] == "${Id}")
  |> filter(fn: (r) => r["_field"] == "err" or r["_field"] == "Con" or r["_field"] == "Cenergy_on" or r["_field"] == "Scompress_on" or r["_field"] == "Sfun_in_on" or r["_field"] == "Sfun_out_on")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;

  let qintemper = `from(bucket: "yjy")
  |> range(${timestring})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["ID"] == "${Id}")
  |> filter(fn: (r) => r["_field"] == "Ctemper" or r["_field"] == "Stemper_in" or r["_field"] == "Stemper_out" or r["_field"] == "Stemper_in_pipe" or r["_field"] == "Stemper_ex")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinhz = `from(bucket: "yjy")
  |> range(${timestring})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["ID"] == "${Id}")
  |> filter(fn: (r) => r["_field"] == "CcompressHz" or r["_field"] == "Scompress_hz")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qina = `from(bucket: "yjy")
  |> range(${timestring})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["ID"] == "${Id}")
  |> filter(fn: (r) => r["_field"] == "SDC_A")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinv = `from(bucket: "yjy")
  |> range(${timestring})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["ID"] == "${Id}")
  |> filter(fn: (r) => r["_field"] == "SDC_V")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinw = `from(bucket: "yjy")
  |> range(${timestring})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["ID"] == "${Id}")
  |> filter(fn: (r) => r["_field"] == "SDC_W")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinerr = `from(bucket: "yjy")
  |> range(${timestring})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["ID"] == "${Id}")
  |> filter(fn: (r) => r["_field"] == "Etemper_in" or r["_field"] == "Etemper_out" or r["_field"] == "Etemper_in_pipe" or r["_field"] == "Eetemper_out_pipe" or r["_field"] == "Etemper_ex" or r["_field"] == "Efun_in" or r["_field"] == "Efun_out" or r["_field"] == "Ec_m_d" or r["_field"] == "Ec_m_o" or r["_field"] == "Eovercurrent" or r["_field"] == "Eoverpower" or r["_field"] == "Eovervoltage_m" or r["_field"] == "Elowvoltage_m" or r["_field"] == "Eovercurrent_w" or r["_field"] == "Elow_v_Battery" or r["_field"] == "Ehigh_v_Battery" or r["_field"] == "Eexhaust" or r["_field"] == "Efreezing" or r["_field"] == "Efluorine")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  return request<API.InfluxDataAllRes>('http://greeshare.gree.com/v1/api/getProductStatesAll', {
    // request<API.Response>('http://192.168.3.81:8088/v1/api/influx', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: {
     // times: t, // 时间：秒
     // deviceImsi: deviceImsi,
      qinsw:qinsw,
      qintemper:qintemper,
      qinhz:qinhz,
      qina:qina,
      qinv:qinv,
      qinw:qinw,
      qinerr:qinerr,
    },
  });
}

export async function getProductStatesAllViewByImsi(deviceImsi: string, viewValues: API.ViewValues) :Promise<API.InfluxDataAllRes> {
  let timestring:string;
  if (viewValues.viewMode===1) {
    timestring = `start: -${viewValues.forepartTime}m`;
  }else {
    timestring =`start: ${viewValues.startTime}, stop: ${viewValues.stopTime}`
  }
/*   from(bucket:"example-bucket")
  |> range(start: 1527031800, stop: 1527033600)
  // ...
   */
  let qinsw = `from(bucket: "yjy")
  |> range(${timestring})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "err" or r["_field"] == "Con" or r["_field"] == "Cenergy_on" or r["_field"] == "Scompress_on" or r["_field"] == "Sfun_in_on" or r["_field"] == "Sfun_out_on")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;

  let qintemper = `from(bucket: "yjy")
  |> range(${timestring})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "Ctemper" or r["_field"] == "Stemper_in" or r["_field"] == "Stemper_out" or r["_field"] == "Stemper_in_pipe" or r["_field"] == "Stemper_ex")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinhz = `from(bucket: "yjy")
  |> range(${timestring})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "CcompressHz" or r["_field"] == "Scompress_hz")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qina = `from(bucket: "yjy")
  |> range(${timestring})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "SDC_A")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinv = `from(bucket: "yjy")
  |> range(${timestring})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "SDC_V")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinw = `from(bucket: "yjy")
  |> range(${timestring})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "SDC_W")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  let qinerr = `from(bucket: "yjy")
  |> range(${timestring})
  |> filter(fn: (r) => r["_measurement"] == "ZC")
  |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
  |> filter(fn: (r) => r["_field"] == "Etemper_in" or r["_field"] == "Etemper_out" or r["_field"] == "Etemper_in_pipe" or r["_field"] == "Eetemper_out_pipe" or r["_field"] == "Etemper_ex" or r["_field"] == "Efun_in" or r["_field"] == "Efun_out" or r["_field"] == "Ec_m_d" or r["_field"] == "Ec_m_o" or r["_field"] == "Eovercurrent" or r["_field"] == "Eoverpower" or r["_field"] == "Eovervoltage_m" or r["_field"] == "Elowvoltage_m" or r["_field"] == "Eovercurrent_w" or r["_field"] == "Elow_v_Battery" or r["_field"] == "Ehigh_v_Battery" or r["_field"] == "Eexhaust" or r["_field"] == "Efreezing" or r["_field"] == "Efluorine")
  |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
  |> yield()
  `;
  return request<API.InfluxDataAllRes>('http://greeshare.gree.com/v1/api/getProductStatesAll', {
    // request<API.Response>('http://192.168.3.81:8088/v1/api/influx', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: {
     // times: t, // 时间：秒
     // deviceImsi: deviceImsi,
      qinsw:qinsw,
      qintemper:qintemper,
      qinhz:qinhz,
      qina:qina,
      qinv:qinv,
      qinw:qinw,
      qinerr:qinerr,
    },
  });
}

// 风机转速
export async function getProductStates_Fan(t: number, deviceImsi: string) {
  // let barcode = "123456789";
  // let deviceImsi = "46006304228299"
  let qin = `from(bucket: "yjy")
      |> range(start: -${t}h)
      |> filter(fn: (r) => r["_measurement"] == "ZC")
      |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
      |> filter(fn: (r) => r["_field"] == "Cfun" or r["_field"] == "Sfun_in_speed" or r["_field"] == "Sfun_out_speed")
      |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
      |> yield()
  `;
  console.log(qin);
  return influx_request(qin);
}

// 电流量
export async function getProductStates_A(t: number, deviceImsi: string) {
  // let barcode = "123456789";
  // let deviceImsi = "46006304228299"
  let qin = `from(bucket: "yjy")
      |> range(start: -${t}h)
      |> filter(fn: (r) => r["_measurement"] == "ZC")
      |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
      |> filter(fn: (r) => r["_field"] == "SDC_A")
      |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
      |> yield()
  `;
  console.log(qin);
  return influx_request(qin);
}

// 电压量
export async function getProductStates_V(t: number, deviceImsi: string) {
  // let barcode = "123456789";
  // let deviceImsi = "46006304228299"
  let qin = `from(bucket: "yjy")
      |> range(start: -${t}h)
      |> filter(fn: (r) => r["_measurement"] == "ZC")
      |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
      |> filter(fn: (r) => r["_field"] == "SDC_V")
      |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
      |> yield()
  `;
  console.log(qin);
  return influx_request(qin);
}

// 功率
export async function getProductStates_W(t: number, deviceImsi: string) {
  // let barcode = "123456789";
  // let deviceImsi = "46006304228299"
  let qin = `from(bucket: "yjy")
      |> range(start: -${t}h)
      |> filter(fn: (r) => r["_measurement"] == "ZC")
      |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
      |> filter(fn: (r) => r["_field"] == "SDC_W")
      |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
      |> yield()
  `;
  //console.log(qin);
  return influx_request(qin);
}

// 故障
export async function getProductStates_Err(t: number, deviceImsi: string) {
  let qin = `from(bucket: "yjy")
      |> range(start: -${t}h)
      |> filter(fn: (r) => r["_measurement"] == "ZC")
      |> filter(fn: (r) => r["DeviceImsi"] == "${deviceImsi}")
      |> filter(fn: (r) => r["_field"] == "Etemper_in" or r["_field"] == "Etemper_out" or r["_field"] == "Etemper_in_pipe" or r["_field"] == "Eetemper_out_pipe" or r["_field"] == "Etemper_ex" or r["_field"] == "Efun_in" or r["_field"] == "Efun_out" or r["_field"] == "Ec_m_d" or r["_field"] == "Ec_m_o" or r["_field"] == "Eovercurrent" or r["_field"] == "Eoverpower" or r["_field"] == "Eovervoltage_m" or r["_field"] == "Elowvoltage_m" or r["_field"] == "Eovercurrent_w" or r["_field"] == "Elow_v_Battery" or r["_field"] == "Ehigh_v_Battery" or r["_field"] == "Eexhaust" or r["_field"] == "Efreezing" or r["_field"] == "Efluorine")
      |> drop(columns: ["_measurement","Barcode","DeviceImsi","_start","_stop","result","table"])
      |> yield()
  `;
  //console.log(qin);
  return influx_request(qin);
}

// 获取查询条数
export async function getInfluxDataCount(influxSearchParams: API.InfluxSearchParam) {
  return request<API.InfluxDataNumResp>('http://greeshare.gree.com/v1/api/getInfluxDataCount', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: influxSearchParams,
  });
}

// 按id查询数据条数
export async function getProductInfluxDataCountById(Id: number, viewValues: any) {
  console.log("viewValues:",viewValues);
  const startTime = dayjs(viewValues.dateTimeRange[0], 'YY-MM-DD HH:mm:ss', 'es').unix();
  const stopTime = dayjs(viewValues.dateTimeRange[1], 'YY-MM-DD HH:mm:ss', 'es').unix();
  let influxSearchParams: API.InfluxSearchParams = {
    bucket: "yjy",
    forepartTime: 0, //提前时间，单位分钟
    start: startTime,
    stop: stopTime,
    measurement: "ZC",
    tags: new Map,
    fields: [],
  }
  if (viewValues.viewMode===1) {
    influxSearchParams.forepartTime = viewValues.forepartTime;
  }else {
    influxSearchParams.forepartTime = 0;
    influxSearchParams.start = startTime;
    influxSearchParams.stop = stopTime;
  }

  influxSearchParams.tags.set("ID",Id);
  influxSearchParams.fields[0] = "SDC_A";

  return request<API.InfluxDataNumResp>('http://greeshare.gree.com/v1/api/getInfluxDataCountByParams', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    data: influxSearchParams,
  });
  
}
